<template>
  <div class="detail">
    <el-card class="mycard">
      <el-button v-if="activeName !== 'first'" @click="toPrint" type="text" class="mybtn"
        ><i class="el-icon-printer"></i
      ></el-button>
      <el-tabs v-model="activeName">
        <el-tab-pane label="登录账户设置" name="first">
          <el-form class="myform" :model="form" label-width="100px">
            <el-form-item label="姓名">
              <el-input v-model="form.username"></el-input>
            </el-form-item>
            <el-form-item label="密码">
              <el-input v-model="form.password" type="password"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="submit" type="primary">更新</el-button>
              <el-button>取消</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情" name="second">
          <userInfo></userInfo>
        </el-tab-pane>
        <el-tab-pane label="岗位信息" name="third">
          <jobInfo></jobInfo>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import { getBaseInfoApi, saveBaseInfoApi } from '@/api/employees'
export default {
  data () {
    return {
      activeName: 'first',
      // 表单数据源
      form: {
        username: '',
        password: ''
      },
      // 接收 id
      id: this.$route.query.id
    }
  },
  methods: {
    toPrint () {
      // 跳转到打印页面
      this.$router.push(`/employees/print?id=${this.id}&active=${this.activeName}`)
    },
    // 更新数据
    async submit () {
      // 提交数据到服务器
      await saveBaseInfoApi(this.form)
      // 提示成功
      this.$message.success('更新数据成功')
    },
    // 得到用户基本信息
    async getBaseInfo () {
      const res = await getBaseInfoApi(this.id)
      // 保存用户信息
      this.form = res.data
    }
  },
  created () {
    this.getBaseInfo()
  },
  components: {
    userInfo: () => import('../components/user-info.vue'),
    jobInfo: () => import('../components/job-info.vue')
  }
}
</script>

<style lang="scss">
.detail {
  margin: 20px;
  .myform {
    width: 400px;
    margin-top: 50px;
    margin-left: 250px;
  }
  .mycard {
    position: relative;
    .mybtn {
      position: absolute;
      right: 20px;
      font-size: 20px;
      z-index: 99999;
    }
  }
}
</style>
